package com.ap.core.validation.message.rules;

import com.ap.core.cxf.jaxb.common.DeliveryType;
import com.ap.core.cxf.jaxb.rtp.TransactionCheckOutType;
import com.ap.core.dao.FinancialInstitutionDao;
import com.ap.core.dao.QuickPayDeliveryAddressDao;
import com.ap.core.domain.FinancialInstitution;
import com.ap.core.domain.QuickPayDeliveryAddress;
import com.ap.core.message.managepayment.info.ConfirmPaymentRequestMessageInfo;
import com.ap.core.validation.message.confirmpayment.ConfirmPaymentMessage;
import com.ap.core.validation.message.info.MessageValidationInfo;
import com.ap.core.validation.message.info.RTPOriginalInfoForConfirmPayment;
import com.ap.core.validation.rules.AbstractValidationRule;

/**
 * User: Ranjit.Singh
 * Check if any field is different as compared to original confirm payment message
 */
public class RV0021Rule extends AbstractValidationRule<MessageValidationInfo<ConfirmPaymentRequestMessageInfo, RTPOriginalInfoForConfirmPayment>> {

    private FinancialInstitutionDao financialInstitutionDao;
    private QuickPayDeliveryAddressDao quickPayDeliveryAddressDao;


    @Override
    public boolean validate(MessageValidationInfo<ConfirmPaymentRequestMessageInfo, RTPOriginalInfoForConfirmPayment> infoObject) {
        final ConfirmPaymentMessage currentConfirmPaymentMessage = new ConfirmPaymentMessage(infoObject.getBoundObject());
        QuickPayDeliveryAddress quickPayDeliveryAddress = null;
        if(TransactionCheckOutType.QKPACHK == infoObject.getOriginalInfo().getTransactionCheckOutType() &&  DeliveryType.DELTAD == infoObject.getOriginalInfo().getDeliveryType()){
            quickPayDeliveryAddress = quickPayDeliveryAddressDao.getReadOnlyQuickPayDeliveryAddressByAPTransactionId(infoObject.getBoundObject().getPaymtDetails().getApTransactionId());
        }

        final FinancialInstitution financialInstitution = financialInstitutionDao.findById(infoObject.getOriginalInfo().getFIid());
        final String fiIdentification = null == financialInstitution ? null : financialInstitution.getFiIdentification();
        final ConfirmPaymentMessage originalConfirmPaymentMessage = new ConfirmPaymentMessage(infoObject.getOriginalInfo().getOriginalOrderPayment(), quickPayDeliveryAddress, fiIdentification);
        return currentConfirmPaymentMessage.equals(originalConfirmPaymentMessage);
    }

    public void setFinancialInstitutionDao(FinancialInstitutionDao financialInstitutionDao) {
        this.financialInstitutionDao = financialInstitutionDao;
    }

    public void setQuickPayDeliveryAddressDao(QuickPayDeliveryAddressDao quickPayDeliveryAddressDao) {
        this.quickPayDeliveryAddressDao = quickPayDeliveryAddressDao;
    }
}
